#include "ShopCacheData.h"


ShopCacheData* ShopCacheData::mInstance = NULL;

void ShopCacheData::CreateInstance()
{
	if (mInstance == nullptr)
	{
		mInstance = new ShopCacheData();
	}
}

ShopCacheData* ShopCacheData::GetInstance()
{
	return mInstance;
}

ShopCacheData::ShopCacheData()
	: m_maxCount(0)
{
}

ShopCacheData::~ShopCacheData()
{
	Clear();
}

bool ShopCacheData::Reload()
{
	_safe_guard_begin;

	Clear();

	Initialization();

	_safe_guard_end;

	return false;
}

void ShopCacheData::Clear()
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.begin();
	for (; itor != m_mapCacheData.end(); ++itor)
	{
		_safe_delete(itor->second);
	}

	m_mapCacheData.clear();

	_safe_guard_end;
}

bool ShopCacheData::Initialization()
{
	_safe_guard_begin;

	Json::Value _jv = ReadJsonFile("./data/shop/shop.json");
	if (_jv == Json::nullValue)
	{
		LOG_ERROR("default","ReadJsonFile -> data/shop/shop.json failed ......");
		return false;
	}

	m_maxCount = _jv.size();

	Json::Value::Members _tmp = _jv.getMemberNames();
	uint32_t _size = (uint32_t)(_tmp.size());
	for (uint32_t i = 0; i < _size; ++i)
	{
		uint32_t _idx = (uint32_t)atoi(_tmp[i].c_str());

		AddData(_idx, _jv[_tmp[i]]);
	}

	return true;

	_safe_guard_end;

	return false;
}


void ShopCacheData::AddData(uint32_t id, Json::Value& jm)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor == m_mapCacheData.end())
	{
		Shop* p = new Shop;
		_safe_check(p);

		p->mId = jm["id"].asUInt();
		p->mGoodsNum = jm["goods_num"].asUInt();

		// 
		Json::Value soulNum = jm["soul_num"];
		uint32_t sz = soulNum.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mSoulNum[i] = soulNum[i].asUInt();
		}

		Json::Value soulStone = jm["soul_stone"];
		sz = soulStone.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			Json::Value vv = soulStone[i];
			uint32_t szz = vv.size();
			UInt32_5x c;
			for (uint32_t n = 0; n < szz; ++n)
			{
				c.mValue[n] = vv[n].asUInt();
			}
			p->mSoulStone.push_back(c);
		}

		// 
		Json::Value expNum = jm["exp_num"];
		sz = expNum.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mExpNum[i] = expNum[i].asUInt();
		}

		Json::Value expMaterial = jm["exp_material"];
		sz = expMaterial.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			Json::Value vv = expMaterial[i];
			uint32_t szz = vv.size();
			UInt32_5x c;
			for (uint32_t n = 0; n < szz; ++n)
			{
				c.mValue[n] = vv[n].asUInt();
			}

			p->mExpMaterial.push_back(c);
		}

		// 
		Json::Value propNum = jm["prop_num"];
		sz = propNum.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mPropNum[i] = propNum[i].asUInt();
		}

		Json::Value prop = jm["prop"];
		sz = prop.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			Json::Value vv = prop[i];
			uint32_t szz = vv.size();
			UInt32_5x c;
			for (uint32_t n = 0; n < szz; ++n)
			{
				c.mValue[n] = vv[n].asUInt();
			}
			p->mProp.push_back(c);
		}

		// 
		Json::Value propPieceNum = jm["prop_piece_num"];
		sz = propPieceNum.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mPropPieceNum[i] = propPieceNum[i].asUInt();
		}

		Json::Value propPiece = jm["prop_piece"];
		sz = propPiece.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			Json::Value vv = propPiece[i];
			uint32_t szz = vv.size();
			UInt32_5x c;
			for (uint32_t n = 0; n < szz; ++n)
			{
				c.mValue[n] = vv[n].asUInt();
			}
			p->mPropPiece.push_back(c);
		}

		// 
		Json::Value diamondPropNum = jm["diamond_prop_num"];
		sz = diamondPropNum.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mDiamondPropNum[i] = diamondPropNum[i].asUInt();
		}

		Json::Value diamondProp = jm["diamond_prop"];
		sz = diamondProp.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			Json::Value vv = diamondProp[i];
			uint32_t szz = vv.size();
			UInt32_5x c;
			for (uint32_t n = 0; n < szz; ++n)
			{
				c.mValue[n] = vv[n].asUInt();
			}

			p->mDiamondProp.push_back(c);
		}

		Json::Value goldProp = jm["gold_prop"];
		sz = goldProp.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			Json::Value vv = goldProp[i];
			uint32_t szz = vv.size();
			UInt32_5x c;
			for (uint32_t n = 0; n < szz; ++n)
			{
				c.mValue[n] = vv[n].asUInt();
			}
			p->mGoldProp.push_back(c);
		}

		// 
		Json::Value refreshPoint = jm["refresh_point"];
		sz = refreshPoint.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mRefreshPoint[i] = refreshPoint[i].asUInt();
		}

		m_mapCacheData[id] = p;
	}

	_safe_guard_end;
}

Shop* ShopCacheData::GetData(uint32_t id)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor != m_mapCacheData.end())
	{
		return itor->second;
	}
	_safe_guard_end;

	return nullptr;
}

bool ShopCacheData::GetDataJsonString(uint32_t id, Json::Value& _jm)
{
	_safe_guard_begin;

	Shop* p = GetData(id);
	if (nullptr == p)
	{
		LOG_ERROR("default","Error13");
		return false;
	}

	_jm.clear();

	_jm["id"] = Json::Value(p->mId);


	_safe_guard_end;

	return true;
}

uint32_t ShopCacheData::GetDataCount()
{
	_safe_guard_begin;

	return m_maxCount;

	_safe_guard_end;
}

